#lang racket
(define (Mb-to-B n) (* n 1024 1024))
(define MAX-BYTES (Mb-to-B 64))
(custodian-limit-memory (current-custodian) MAX-BYTES)

(define tolerance 0.0000000001)
(define (fixed-point f guess)
	(define (close-enough? value1 value2)
		(< (abs (- value1 value2)) tolerance))
	(define (try current-guess)
		(display current-guess) (newline)
		(let
			((next (f current-guess)))
			(if
				(close-enough? current-guess next)
				next
				(try next))))
	(try guess))

(define (xx x)
	(/ (log 1000) (log x)))

;; defines a value
(define x-power-x-is-1000
	(fixed-point xx 2.0))

(define (average a b)
	(/ (+ a b) 2))

(define x-power-x-is-1000-avg-damping
	(fixed-point
		(lambda (x) (average (xx x) x))
		2.0))

;; prints the value
x-power-x-is-1000
x-power-x-is-1000-avg-damping

; 2.0
; 9.965784284662087
; 3.004472209841214
; 6.279195757507157
; 3.759850702401539
; 5.215843784925895
; 4.182207192401397
; 4.8277650983445906
; 4.387593384662677
; 4.671250085763899
; 4.481403616895052
; 4.6053657460929
; 4.5230849678718865
; 4.577114682047341
; 4.541382480151454
; 4.564903245230833
; 4.549372679303342
; 4.559606491913287
; 4.552853875788271
; 4.557305529748263
; 4.554369064436181
; 4.556305311532999
; 4.555028263573554
; 4.555870396702851
; 4.555315001192079
; 4.5556812635433275
; 4.555439715736846
; 4.555599009998291
; 4.555493957531389
; 4.555563237292884
; 4.555517548417651
; 4.555547679306398
; 4.555527808516254
; 4.555540912917957
; 4.555532270803653
; 4.555537970114198
; 4.555534211524127
; 4.555536690243655
; 4.555535055574168
; 4.5555361336081
; 4.555535422664798
; 4.5555358915186215
; 4.555535582318266
; 4.555535786230128
; 4.555535651754059
; 4.555535740438517
; 4.5555356819527715
; 4.555535720523029
; 4.555535695086664
; 4.555535711861472
; 4.555535700798798
; 4.555535708094425
; 4.555535703283096
; 4.5555357064560775
; 4.555535704363555
; 4.555535705743535
; 4.5555357048334635
; 4.5555357054336385
; 4.555535705037834
; 4.55553570529886
; 4.555535705126719
; 4.555535705240242
; ==> 62 steps

; 2.0
; 5.9828921423310435
; 4.922168721308343
; 4.628224318195455
; 4.568346513136242
; 4.5577305909237005
; 4.555909809045131
; 4.555599411610624
; 4.5555465521473675
; 4.555537551999825
; 4.555536019631145
; 4.555535758730802
; 4.555535714310077
; 4.555535706747033
; 4.555535705459354
; 4.555535705240114
; 4.555535705165376
; 4.555535705202788
; ==> 18 steps